#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<string>
using namespace std;


class Solution {
public:
	string addStrings(string num1, string num2)
	{
		string::reverse_iterator rN1 = num1.rbegin();
		string::reverse_iterator rN2 = num2.rbegin();
		string str;
		//int size = (num1.size()>num2.size()? num1.size():num2.size())+1;
		//str.reserve(3);
		int next = 0;
		//int length = size-1;
		while (rN1 != num1.rend() || rN2 != num2.rend())
		{
			int n1 = rN1 != num1.rend() ? (*rN1 - '0') : 0;
			int n2 = rN2 != num2.rend() ? (*rN2 - '0') : 0;
			int ret = n1 + n2 + next;

			if (ret>9)
			{
				ret %= 10;
				next = 1;
			}
			else
			{
				next = 0;
			}

			str += (ret + '0');

			if (rN1 != num1.rend())
				rN1++;

			if (rN2 != num2.rend())
				rN2++;
		}

		if (next == 1)
			str += '1';

		reverse(str.begin(), str.end());


		return str;
	}
};




int main()
{
	string n1("456");
	string n2("877");
	Solution s;
	cout<<s.addStrings(n1, n2)<<endl;
	return 0;
}